<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.username" clearable size="default" placeholder="请输入用户名称"
					style="max-width: 180px">
				</el-input>
				<el-button size="default" type="primary" class="ml10" @click="getUserPage">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddUser">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增用户
				</el-button>
			</div>
			<el-table :data="state.tableData.data" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="username" label="账户名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="tenantName" label="所属租户" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="用户状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditUser(scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum" background
				v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total">
			</el-pagination>
		</el-card>
		<AdminDialog @refresh="getUserPage()" ref="adminDialogRef" />
	</div>
</template>

<script setup name="tenantAdmin">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import AdminDialog from '/@/views/tenant/admin/dialog.vue';
import { useTenantApi } from "/@/api/tenant";
const tenantApi = useTenantApi()



const adminDialogRef = ref();
const editUserRef = ref();
const state = reactive ({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
			username: ''
		},
	},
});
const getUserPage = () => {
	let params = {
		"page": state.tableData.param.pageNum,
		"limit": state.tableData.param.pageSize,
		"username": state.tableData.param.username,
	}
	tenantApi.tenantSuperAdminPage(params).then(res => {
		state.tableData.data = res.data.list;
		state.tableData.total = res.data.count;
	})
}
// 打开新增用户弹窗
const onOpenAddUser = () => {
	adminDialogRef.value.openDialog('add');
};
// 打开修改用户弹窗
const onOpenEditUser = (row) => {
	adminDialogRef.value.openDialog('edit', row.id);
};
// 删除用户
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.username}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			let params = {
				"id": row.id,
				'tenantId': row.tenantId
			}
			tenantApi.tenantSuperAdminDelete(params).then(() => {
				getUserPage();
				ElMessage.success('删除成功');
			})
		})
		.catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.pageSize = val;
	getUserPage();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.pageNum = val;
	getUserPage();
};
// 页面加载时
onMounted(() => {
	getUserPage();
});
</script>